/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Client;
//import com.easynth.lookandfeel.EaSynthLookAndFeel;
import java.awt.*;
import javax.swing.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.util.logging.*;
import rmip.ServerInt;

public class Client extends JFrame {

ServerInt si;
int logFlag=0;
int mailFlag=0;
int passFlag=0;
Registry r;
String email;

public void userLogin(){

  String hashed;
  String email=tEmail.getText();
  String pw=tPasswd.getText();
    
    try {
      boolean loginStatus=si.login(email, pw);
      
      if(loginStatus){
        ClientImp cli =new ClientImp();
        si.signUp(email, cli);
        ChatWin ch = new ChatWin(email,si,cli);
        cli.setuserWindow(ch);
        ch.setVisible(true);
        dispose();
        si.showRequests(email);
      }
      else{
         errLogin.setVisible(true);
      }
    } catch (RemoteException ex) {
      ex.printStackTrace();
    }
  }
  
  public String getEmail(){ return email;}
    /**
     * Creates new form login
     */
    public Client()  {
        initComponents();
       
    try {
        r = LocateRegistry.getRegistry("127.0.0.1", 7777);
    } catch (RemoteException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("cannout getRegistry");
    }
        try {
            si=(ServerInt)r.lookup("ChatServer");        
        }
        catch (RemoteException | NotBoundException ex) {
        JOptionPane.showMessageDialog(null, "Sorry serever is down");
        System.exit(0);
            ex.printStackTrace();
            
        }
        errMail.setVisible(false);
        errPasswd.setVisible(false);
        errLogin.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        tEmail = new javax.swing.JTextField();
        jPanel10 = new javax.swing.JPanel();
        errMail = new javax.swing.JLabel();
        jPanel15 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        tPasswd = new javax.swing.JPasswordField();
        jPanel6 = new javax.swing.JPanel();
        errPasswd = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        login = new javax.swing.JButton();
        jPanel18 = new javax.swing.JPanel();
        errLogin = new javax.swing.JLabel();
        jPanel16 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chat w Laban");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFont(new java.awt.Font("Segoe UI Symbol", 0, 18)); // NOI18N
        setMinimumSize(new java.awt.Dimension(350, 700));
        setPreferredSize(new java.awt.Dimension(350, 700));

        jPanel1.setLayout(new java.awt.GridLayout(4, 1));

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel5.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                jPanel5ComponentHidden(evt);
            }
        });

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/logo.jpg"))); // NOI18N
        jPanel5.add(logo);

        jPanel2.add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel7.setLayout(new java.awt.GridLayout(6, 1));

        jLabel6.setFont(new java.awt.Font("Segoe UI Symbol", 0, 18)); // NOI18N
        jLabel6.setText("E-mail");
        jPanel14.add(jLabel6);

        jPanel7.add(jPanel14);

        tEmail.setMinimumSize(new java.awt.Dimension(30, 20));
        tEmail.setPreferredSize(new java.awt.Dimension(220, 30));
        tEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tEmailActionPerformed(evt);
            }
        });
        tEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tEmailFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                tEmailFocusLost(evt);
            }
        });
        jPanel9.add(tEmail);

        jPanel7.add(jPanel9);

        errMail.setForeground(new java.awt.Color(204, 0, 0));
        errMail.setText("* Enter a valid email address");
        errMail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        errMail.setVerifyInputWhenFocusTarget(false);
        jPanel10.add(errMail);

        jPanel7.add(jPanel10);

        jLabel7.setFont(new java.awt.Font("Segoe UI Symbol", 0, 18)); // NOI18N
        jLabel7.setText("Password");
        jPanel15.add(jLabel7);

        jPanel7.add(jPanel15);

        jPanel3.add(jPanel7, java.awt.BorderLayout.PAGE_START);

        jPanel1.add(jPanel3);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel13.setLayout(new java.awt.GridLayout(5, 1));

        tPasswd.setPreferredSize(new java.awt.Dimension(220, 30));
        tPasswd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tPasswdActionPerformed(evt);
            }
        });
        tPasswd.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tPasswdFocusLost(evt);
            }
        });
        jPanel8.add(tPasswd);

        jPanel13.add(jPanel8);

        errPasswd.setForeground(new java.awt.Color(204, 0, 0));
        errPasswd.setText("* Password is required");
        errPasswd.setVerifyInputWhenFocusTarget(false);
        jPanel6.add(errPasswd);

        jPanel13.add(jPanel6);

        login.setFont(new java.awt.Font("Segoe UI Symbol", 0, 14)); // NOI18N
        login.setText("Sign in");
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });
        jPanel11.add(login);

        jPanel13.add(jPanel11);

        errLogin.setForeground(new java.awt.Color(204, 0, 0));
        errLogin.setText("* Invalid email or password please try again");
        errLogin.setVerifyInputWhenFocusTarget(false);
        jPanel18.add(errLogin);

        jPanel13.add(jPanel18);

        jPanel4.add(jPanel13, java.awt.BorderLayout.PAGE_START);

        jPanel1.add(jPanel4);

        jPanel16.setLayout(new java.awt.BorderLayout());

        jPanel17.setLayout(new java.awt.GridLayout(6, 1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Don't have account yet ?");
        jPanel12.add(jLabel2);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 255));
        jLabel3.setText("Sign Up");
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel3MouseExited(evt);
            }
        });
        jPanel12.add(jLabel3);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("now");
        jPanel12.add(jLabel5);

        jPanel17.add(jPanel12);

        jPanel16.add(jPanel17, java.awt.BorderLayout.PAGE_START);

        jPanel1.add(jPanel16);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tPasswdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tPasswdActionPerformed
        // TODO add your handling code here:
        login.doClick();
    }//GEN-LAST:event_tPasswdActionPerformed

    private void tEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tEmailActionPerformed
        // TODO add your handling code here:
        login.doClick();
    }//GEN-LAST:event_tEmailActionPerformed

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
        // TODO add your handling code here:
        String email=tEmail.getText();
        String regex = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        if(email.matches(regex)){
            mailFlag=1;
            errMail.setVisible(false);
        } else {
            mailFlag=0;
            errMail.setVisible(true);
        }
        String pass=tPasswd.getText();
        String regex1 = "^[A-Za-z0-9/.]+$";
        if(pass.matches(regex1)){
            passFlag=1;
            errPasswd.setVisible(false);
        } else {
            passFlag=0;
            errPasswd.setVisible(true);
        }
        if(mailFlag==1&&passFlag==1){
            userLogin();
        }
    }//GEN-LAST:event_loginActionPerformed

    private void jPanel5ComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel5ComponentHidden
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel5ComponentHidden

    private void tEmailFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tEmailFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_tEmailFocusGained

    private void tEmailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tEmailFocusLost
        // TODO add your handling code here:
        String email=tEmail.getText();
        String regex = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        if(email.matches(regex)){
            mailFlag=1;
            errMail.setVisible(false);
        } else {
            mailFlag=0;
            errMail.setVisible(true);
        }
    }//GEN-LAST:event_tEmailFocusLost

    private void tPasswdFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tPasswdFocusLost
        // TODO add your handling code here:
        String pass=tPasswd.getText();
        String regex = "^[A-Za-z0-9/.]+$";
        if(pass.matches(regex)){
            passFlag=1;
            errPasswd.setVisible(false);
        } else {
            passFlag=0;
            errPasswd.setVisible(true);
        }
    }//GEN-LAST:event_tPasswdFocusLost

    private void jLabel3MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseExited
        // TODO add your handling code here:
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_jLabel3MouseExited

    private void jLabel3MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseEntered
        // TODO add your handling code here:
        setCursor(new Cursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_jLabel3MouseEntered

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        // TODO add your handling code here:
        new SignUp(si).setVisible(true);
    }//GEN-LAST:event_jLabel3MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

      /* try {
		final EaSynthLookAndFeel easynthLAF = new EaSynthLookAndFeel();
		UIManager.setLookAndFeel(easynthLAF);
	} catch (Exception e) {
		e.printStackTrace();
	}*/
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Client().setVisible(true);
            }
        });
    }



    // End of variables declaration                   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel errLogin;
    private javax.swing.JLabel errMail;
    private javax.swing.JLabel errPasswd;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JButton login;
    private javax.swing.JLabel logo;
    private javax.swing.JTextField tEmail;
    private javax.swing.JPasswordField tPasswd;
    // End of variables declaration//GEN-END:variables

}
